package com.zygh.lz.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zygh.lz.entity.VehicleCoordinate;
import com.zygh.lz.entity.vo.ScheduVehicleVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface VehicleCoordinateMapper extends BaseMapper<VehicleCoordinate> {

    /**
     * 根据车牌号码和车牌颜色查询车辆定位信息，定位信息一分钟上传一次，这里查1分钟内的
     * 查询gps_time为当天的
     */
    @Select("select * from vehicle_coordinate where number=#{number} and color=#{color} and gps_time like CONCAT(CURDATE(),'%')" +
            " and TIMESTAMPDIFF(MINUTE,gmt_create,NOW()) <= 10 limit 1")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "number", property = "number", jdbcType = JdbcType.VARCHAR),
            @Result(column = "color", property = "color", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gps_time", property = "gps_time", jdbcType = JdbcType.VARCHAR),
            @Result(column = "lng", property = "lng", jdbcType = JdbcType.VARCHAR),
            @Result(column = "lat", property = "lat", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gmt_create", property = "gmt_create", jdbcType = JdbcType.VARCHAR)
    })
    VehicleCoordinate getByNumber(@Param("number") String number,
                                  @Param("color") String color);

    /**
     * 获取清障车车辆的实时定位信息
     */
    @Select("SELECT ve.id,si.driver,si.number,si.driver_phone,si.color,ve.lng,ve.lat FROM `vehicle_coordinate` ve " +
            "left join scheduling_information si on ve.number=si.number " +
            "where ve.gps_time like CONCAT(CURDATE(),'%') and TIMESTAMPDIFF(MINUTE,ve.gmt_create,NOW()) <= 10 " +
            "and ve.number=#{number} and ve.color=#{color} limit 1")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "driver", property = "driver", jdbcType = JdbcType.VARCHAR),
            @Result(column = "number", property = "number", jdbcType = JdbcType.VARCHAR),
            @Result(column = "driver_phone", property = "driverPhone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "color", property = "color", jdbcType = JdbcType.VARCHAR),
            @Result(column = "lng", property = "lng", jdbcType = JdbcType.VARCHAR),
            @Result(column = "lat", property = "lat", jdbcType = JdbcType.VARCHAR)
    })
    ScheduVehicleVO getInfo(@Param("number") String number,
                            @Param("color") String color);

    /**
     * 定时删除昨天的车辆定位数据
     */
    @Delete("delete from vehicle_coordinate where gmt_create < #{time}")
    void deleteVehicleCoordinate(@Param("time") String time);
}
